package cn.sym.org.controller;

import cn.sym.basic.exception.MyException;
import cn.sym.basic.util.AjaxResult;
import cn.sym.basic.util.ExcelUtil;
import cn.sym.basic.util.PageList;
import cn.sym.org.aspect.anno.Operation;
import cn.sym.org.domain.Shop;
import cn.sym.org.domain.ShopAuditLog;
import cn.sym.org.query.ShopQuery;
import cn.sym.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlemynt(@RequestBody Shop shop){
        try {
            System.out.println(shop);
            shopService.settlement(shop);
            return AjaxResult.my();
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }
    /**
     * 店铺入驻驳回之后点击链接修改信息
     * @param shop
     */
    @PostMapping("/settlement2")
    public AjaxResult settlemynt2(@RequestBody Shop shop){
        try {
            System.out.println(shop);
            shopService.settlement2(shop);
            return AjaxResult.my();
        }catch (MyException e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
 /*   @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setMessage("保存对象失败！"+e.getMessage());
        }
    }*/
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Operation("批量删除")
    @PatchMapping
    public AjaxResult remove(@RequestBody List<Long> ids){
        try {
            shopService.removeMore(ids);
            return AjaxResult.my();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("网络错误，请稍后再试！！1");
        }
    }
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺信息人工审核通过
     * @param shopAuditLog  审核日志
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }
    /**
     * 店铺信息人工审核驳回
     * @param shopAuditLog  审核日志
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * Excel导出数据
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Excel导入数据
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        list.forEach(System.out::println);
        shopService.addMore(list);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]

    }
}
